import { defineStore } from 'pinia'

export const useTagsViewStore = defineStore('tagsView', {
  state: () => {
    return {
      visitedViews: [],
      cachedViews: []
    }
  },
  actions: {
    ADD_VISITED_VIEW(view: RouterView) {
      this.$patch(state => {
        if (state.visitedViews.some((v: any) => v.path === view.path)) return
        const _view = Object.assign({}, view, {
          title: view.meta.title || 'no-name'
        })
        state.visitedViews.push(_view)
      })
    },
    ADD_CACHED_VIEW(view: any) {
      this.$patch(state => {
        if (state.cachedViews.includes(view.name)) return
        if (!view.meta.noCache) {
          state.cachedViews.push(view.name)
        }
      })

    },
    addView(view: RouterView) {
      this.addVisitedView(view)
      this.addCachedView(view)
    },
    addVisitedView(view: RouterView) {
      this.ADD_VISITED_VIEW(view)
    },
    addCachedView(view: RouterView) {
      this.ADD_CACHED_VIEW(view)
    },
    //删除
    DEL_VISITED_VIEW(view: RouterView) {
      this.$patch(state => {
        for (const [i, v] of state.visitedViews.entries()) {
          if (v.path === view.path) {
            state.visitedViews.splice(i, 1)
            break
          }
        }
      })
    },
    DEL_CACHED_VIEW(view) {
      this.$patch(state => {
        const index = state.cachedViews.indexOf(view.name)
        index > -1 && state.cachedViews.splice(index, 1)
      })
    },
    delView(view) {
      return new Promise(resolve => {
        this.delVisitedView(view)
        this.delCachedView(view)
        this.$patch(state => {
          resolve({
            visitedViews: [...state.visitedViews],
            cachedViews: [...state.cachedViews]
          })
        })
      })
    },
    delVisitedView(view) {
      return new Promise(resolve => {
        this.DEL_VISITED_VIEW(view)
        this.$patch(state => {
          resolve([...state.visitedViews])
        })
      })
    },
    delCachedView(view) {
      return new Promise(resolve => {
        this.DEL_CACHED_VIEW(view)
        this.$patch(state => {
          resolve([...state.cachedViews])
        })
      })
    },
    //删除其他所有
    DEL_OTHERS_VISITED_VIEWS(view) {
      this.$patch(state => {
        state.visitedViews = state.visitedViews.filter(v => {
          return v.meta.affix || v.path === view.path
        })
      })
    },
    DEL_OTHERS_CACHED_VIEWS(view) {
      this.$patch(state => {
        const index = state.cachedViews.indexOf(view.name)
        if (index > -1) {
          state.cachedViews = state.cachedViews.slice(index, index + 1)
        } else {
          // if index = -1, there is no cached tags
          state.cachedViews = []
        }
      })
    },
    delOthersViews(view) {
      return new Promise(resolve => {
        this.delOthersVisitedViews(view)
        this.delOthersCachedViews(view)
        this.$patch(state => {
          resolve({
            visitedViews: [...state.visitedViews],
            cachedViews: [...state.cachedViews]
          })
        })
      })
    },
    delOthersVisitedViews(view) {
      return new Promise(resolve => {
        this.DEL_OTHERS_VISITED_VIEWS(view)
        this.$patch(state => {
          resolve([...state.visitedViews])
        })
      })
    },
    delOthersCachedViews(view) {
      return new Promise(resolve => {
        this.DEL_OTHERS_CACHED_VIEWS(view)
        this.$patch(state => {
          resolve([...state.cachedViews])
        })
      })
    },
    //删除全部
    DEL_ALL_VISITED_VIEWS() {
      this.$patch(state => {
        // keep affix tags
        const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
        state.visitedViews = affixTags
      })
    },
    DEL_ALL_CACHED_VIEWS() {
      this.$patch(state => {
        state.cachedViews = []
      })
    },
    UPDATE_VISITED_VIEW(view) {
      this.$patch(state => {
        for (let v of state.visitedViews) {
          if (v.path === view.path) {
            v = Object.assign(v, view)
            break
          }
        }
      })
    },
    delAllViews() {
      return new Promise(resolve => {
        this.delAllVisitedViews()
        this.delAllCachedViews()
        this.$patch(state => {
          resolve({
            visitedViews: [...state.visitedViews],
            cachedViews: [...state.cachedViews]
          })
        })
      })
    },
    delAllVisitedViews() {
      return new Promise(resolve => {
        this.DEL_ALL_VISITED_VIEWS()
        this.$patch(state => {
          resolve([...state.visitedViews])
        })
      })
    },
    delAllCachedViews() {
      return new Promise(resolve => {
        this.DEL_ALL_CACHED_VIEWS()
        this.$patch(state => {
          resolve([...state.cachedViews])
        })
      })
    },
    updateVisitedView(view) {
      this.UPDATE_VISITED_VIEW(view)
    }
  }
})